<?php
/**
 * Template Name: Confirm Booking
 */

if ($_REQUEST['form_email']) {
    $userID = wp_create_user($_REQUEST['form_email'], $_REQUEST['form_password'], $_REQUEST['form_email']);
    wp_update_user( array ('ID' => $userID, 'first_name' => $_REQUEST['form_first'], 'last_name' => $_REQUEST['form_last']) ) ;

    $field_key = "field_517436d75c29a"; //Phone
    $value = $_REQUEST['form_phone'];
    update_field($field_key, $value, 'user_'.$userID);

    $creds = array();
    $creds['user_login'] = $_REQUEST['form_email'];
    $creds['user_password'] = $_REQUEST['form_password'];
    $creds['remember'] = true;
    $user = wp_signon( $creds, false );
    if ( is_wp_error($user) ) {
        echo $user->get_error_message();
    }
    $logged = true;
    $user = $userID;
} else {
    $logged = is_user_logged_in();
    $user = get_user_id();
}

get_header(); ?>

<div class="container">
    <? while (have_posts()) : the_post(); ?>

    <?

    $venue = getVenueID($_REQUEST['form_pitch']);
    $pitch = $_REQUEST['form_pitch'];
    $slot = $_REQUEST['form_slot'];
    $date = $_REQUEST['form_date'];
    $price = $_REQUEST['form_price'];
    ?>

    <div>
        <h1>Confirm Booking</h1>

        <div class="row-fluid">
            <div class="span8">
                <div class="card grey">
                    <h2 class="title">Booking Information</h2>

                    <table class="table table-striped table-condensed">
                        <tbody>
                        <tr>
                            <th>Venue</th>
                            <td><? echo get_field('name', $venue) ?></td>
                        </tr>
                        <tr>
                            <th>Pitch</th>
                            <td><? echo get_field('pid', $pitch) ?></td>
                        </tr>
                        <tr>
                            <th>Date</th>
                            <td><? echo date('l, j F Y', strtotime($date)) ?></td>
                        </tr>
                        <tr>
                            <th>Start</th>
                            <td><? echo get_field('start_time', $slot) ?></td>
                        </tr>
                        <tr>
                            <th>End</th>
                            <td><? echo get_field('end_time', $slot) ?></td>
                        </tr>
                        <tr>
                            <th>Price</th>
                            <td><? echo '&pound;' . $price ?></td>
                        </tr>
                        </tbody>
                    </table>

                    <!--
                    <form action="/payment/" method="post" style="margin:0">
                        <input type="hidden" name="form_venue" value="<? echo $venue ?>">
                        <input type="hidden" name="form_pitch" value="<? echo $pitch ?>">
                        <input type="hidden" name="form_slot" value="<? echo $slot ?>">
                        <input type="hidden" name="form_date" value="<? echo $date ?>">
                        <input type="hidden" name="form_user" value="<? echo get_user_id() ?>">
                        <input type="hidden" name="form_price" value="<? echo $price ?>">

                        <a class="btn" href="<? echo get_permalink($pitch) ?>" style="font-weight: 800;">&laquo; Back</a>

                        <? if ($logged) { ?>
                            <input type="submit" class="btn btn-primary pull-right" style="white-space:nowrap;font-weight: 800;" value="Payment &raquo;"/>
                        <? } ?>
                    </form>-->

                    <? if ($logged) { ?>

                    <form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
                        <input type="hidden" name="cmd" value="_xclick">
                        <input type="hidden" name="business" value="GWN9WEVS545ZC">
                        <input type="hidden" name="lc" value="GB">
                        <input type="hidden" name="item_name" value="Pitch Booking">
                        <input type="hidden" name="button_subtype" value="services">
                        <input type="hidden" name="no_note" value="0">
                        <input type="hidden" name="cn" value="Add special instructions to the seller:">
                        <input type="hidden" name="no_shipping" value="1">
                        <input type="hidden" name="rm" value="1">

                        <? $string = "form_venue=$venue&form_pitch=$pitch&form_slot=$slot&form_date=$date&form_user=$user&form_price=$price" ?>
                        <input type="hidden" name="return" value='https://pitchwise.co.uk/authorise/?<? echo $string ?>' />
                        <input type="hidden" name="cancel_return" value="https://pitchwise.co.uk/fail/">
                        <input type="hidden" name="currency_code" value="GBP">
                        <input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHosted">
                        <input type="hidden" name="form_venue" value="<? echo $venue ?>">
                        <input type="hidden" name="form_pitch" value="<? echo $pitch ?>">
                        <input type="hidden" name="form_slot" value="<? echo $slot ?>">
                        <input type="hidden" name="form_date" value="<? echo $date ?>">
                        <input type="hidden" name="form_user" value="<? echo $user ?>">
                        <input type="hidden" name="form_price" value="<? echo $price ?>">
                        <input type="hidden" name="amount" value="<? echo $price ?>">

                        <input type="image" src="https://www.paypalobjects.com/en_US/GB/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal – The safer, easier way to pay online.">
                        <img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1" style="width:1px;">

                    </form>
                    <? } ?>



                </div>

            </div>
            <div class="span4">
                <div class="card grey">
                    <h2 class="title">Your Details</h2>
                    <?php
                    if ($logged) {
                        $user_info = get_userdata($user);
                        ?>

                        <table class="table table-striped table-condensed">
                            <tbody>
                            <tr>
                                <th>Name</th>
                                <td><? echo $user_info->user_firstname . ' ' . $user_info->user_lastname ?></td>
                            </tr>
                            <tr>
                                <th>Email</th>
                                <td><? echo $user_info->user_email ?></td>
                            </tr>
                            <tr>
                                <th>Contact Number</th>
                                <td><? echo get_field('phone', 'user_' . $userID) ?></td>
                            </tr>
                            </tbody>
                        </table>
                        <?
                    } else {
                        ?>
                        <p>If you already have an account, please <a href="#loginModal" data-toggle="modal">log in</a></p>

                        <form action="/confirm-booking/" method="post" style="margin-bottom: 0">
                            <fieldset>
                                <label>First Name<span style="color: red">*</span></label>
                                <input type="text" name="form_first" placeholder="">
                                <label>Last Name<span style="color: red">*</span></label>
                                <input type="text" name="form_last" placeholder="">
                                <label>Email<span style="color: red">*</span></label>
                                <input type="email" name="form_email" placeholder="">
                                <span id="email-help" class="help-block" style="display: none">Example block-level help text here.</span>
                                <label>Password<span style="color: red">*</span></label>
                                <input type="password" name="form_password" placeholder="">
                                <label>Contact Number</label>
                                <input type="text" name="form_phone" placeholder="">

                                <input type="hidden" name="form_venue" value="<? echo $venue ?>">
                                <input type="hidden" name="form_pitch" value="<? echo $pitch ?>">
                                <input type="hidden" name="form_slot" value="<? echo $slot ?>">
                                <input type="hidden" name="form_date" value="<? echo $date ?>">
                                <input type="hidden" name="form_user" value="<? echo $user ?>">
                                <input type="hidden" name="form_price" value="<? echo $price ?>">
                                <hr style="border-top: 1px dashed #eeeeee;border-bottom: 1px dashed #ffffff;margin: 5px 0 15px;">

                                <input id="payment-link" type="submit" disabled="disabled" class="btn btn-primary" style="white-space:nowrap;font-weight: 800;" value="Sign Up! &raquo;"/>
                            </fieldset>
                        </form>

                        <!--
                        <form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
                            <fieldset>
                                <input type="hidden" name="cmd" value="_xclick">
                                <input type="hidden" name="business" value="GWN9WEVS545ZC">
                                <input type="hidden" name="lc" value="GB">
                                <input type="hidden" name="item_name" value="Pitch Booking">
                                <input type="hidden" name="button_subtype" value="services">
                                <input type="hidden" name="no_note" value="0">
                                <input type="hidden" name="cn" value="Add special instructions to the seller:">
                                <input type="hidden" name="no_shipping" value="1">
                                <input type="hidden" name="rm" value="1">
                                <input type="hidden" name="return" value="http://dev.pitchwise.co.uk/authorise/?form_venue=<? echo $venue ?>&form_pitch=<? echo $pitch ?>">
                                <input type="hidden" name="cancel_return" value="http://dev.pitchwise.co.uk/fail/">
                                <input type="hidden" name="currency_code" value="GBP">
                                <input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHosted">
                                <input type="hidden" name="form_venue" value="<? echo $venue ?>">
                                <input type="hidden" name="form_pitch" value="<? echo $pitch ?>">
                                <input type="hidden" name="form_slot" value="<? echo $slot ?>">
                                <input type="hidden" name="form_date" value="<? echo $date ?>">
                                <input type="hidden" name="form_user" value="<? echo $user ?>">
                                <input type="hidden" name="form_price" value="<? echo $price ?>">
                                <input type="hidden" name="amount" value="<? echo $price ?>">


                                <label>First Name<span style="color: red">*</span></label>
                                <input type="text" name="form_first" placeholder="">
                                <label>Last Name<span style="color: red">*</span></label>
                                <input type="text" name="form_last" placeholder="">
                                <label>Email<span style="color: red">*</span></label>
                                <input type="email" name="form_email" placeholder="">
                                <span id="email-help" class="help-block" style="display: none">Example block-level help text here.</span>
                                <label>Password<span style="color: red">*</span></label>
                                <input type="password" name="form_password" placeholder="">
                                <label>Contact Number</label>
                                <input type="text" name="form_phone" placeholder="">
                                <input type="image" src="https://www.paypalobjects.com/en_US/GB/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal – The safer, easier way to pay online.">
                                <img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1">
                            </fieldset>
                        </form>
                        -->

                        <?
                    }
                    ?>


                </div>

            </div>
        </div>


    </div>

    <? endwhile; ?>
</div>

<?php get_footer(); ?>

<script>
    /*
    $(function () {
        if (<?php
    if ($logged) {
        echo 'false';
    } else {
        echo 'true';
    }
    ?>) {
            $('#loginModal').modal({
                backdrop:'static '
            });
            $('#loginModal').on('hide', function () {
                return false;
            });
        }
    });
    */

    PW.confirmBooking.init();
</script>